package com.clock.controller.backstage;


import com.clock.common.domain.R;
import com.clock.domain.dto.UserByManagerDTO;
import com.clock.domain.entity.User;
import com.clock.service.AuthorizationService;
import com.clock.service.UserService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(tags = {"管理员操作用户接口---只允许顶级管理访问"})
@RestController
@RequestMapping("managerUser")
public class ManagerUserController {

    @Resource
    AuthorizationService authorizationService;

    @Resource
    UserService userService;

    @ApiOperation(value = "获得所有用户信息", notes = "只允许顶级管理员访问")
    //无条件分页查询
    @GetMapping("{currentPage}/{pageSize}")
    public R getAllUser(@PathVariable("currentPage") Integer currentPage, @PathVariable("pageSize") Integer pageSize){
        PageInfo<User> allUser = userService.getAllUser(currentPage, pageSize);
        if(allUser!=null){
            return R.ok(allUser);
        }else{
            return R.fail("获取失败");
        }
    }

/*    @ApiOperation(value = "添加角色", notes = "除了角色授权时间，其他参数必须要")
    @PostMapping("set")
    public R setAuthorization(@RequestBody UserNewRoleDTO userNewRoleDTO) {
        boolean setAuthorization = authorizationService.setAuthorization(userNewRoleDTO);
        if(setAuthorization){
            return R.ok("添加权限成功");
        }else
        {
            return R.fail("添加权限失败");
        }
    }*/

    @ApiOperation(value = "更新用户角色信息", notes = "只需要用户id和角色id和授权人id")
    @PostMapping("updateUserRoleId")
    public R updateAuthorization(@RequestBody UserByManagerDTO userByManagerDTO){
        boolean updateAuthorization = authorizationService.updateAuthorization(userByManagerDTO);
        if(updateAuthorization){
            return R.ok("修改权限成功");
        }else
        {
            return R.fail("修改权限失败");
        }
    }

    @ApiOperation(value = "删除用户", notes = "可以只需要用户id")
    @DeleteMapping("deleteUser")
    public R deleteUser(@RequestBody UserByManagerDTO userByManagerDTO){
        boolean deleteById = userService.deleteById(userByManagerDTO.getUserId().intValue());
        if(deleteById){
            return R.ok("删除用户成功");
        }else
        {
            return R.fail("删除用户失败");
        }
    }

}
